# *****************************************************************
# OVERVIEW ####
# *****************************************************************
# 00_GENERAL.R
# Replication Code for:
# Are Rural Attitudes Just Republican?
# PSRM, 2023
# Authors: 
#   Jennifer Lin: jenniferlin2025@u.northwestern.edu
#   Kristin Lunz Trujillo: ktrujillo@hks.harvard.edu
# Created On: 2023 01 27

# ***The following script is intended to be an overview of all
#   the script files present in thsi directory and is intended for 
#   use with quick results. For more in-depth code explainations,
#   consult the respective code files.***

# *****************************************************************
# PACKAGES AND FUNCTIONS ####
# *****************************************************************

# Read in functions file for all the packages and 
#  functions for the analyses with the ANES Data

source("Functions.R")

# *****************************************************************
# LOAD DATA ####
# *****************************************************************

# All of the script files located in this replication directory 
#  requires that you have obtained a copy of the ANES 2020 data
#  from https://electionstudies.org/ and have loaded it into the
#  environment in your R session.

url <- "https://electionstudies.org/"
extension <- "wp-content/uploads/2022/02/"
filename <- "anes_timeseries_2020_stata_20220210.zip"
data_url <- paste0(url, extension, filename)

# Use BrowseURL to download the contents. Unzip them into a 
# folder called `data/ANES2020/` in the same file as this code.

browseURL(data_url)

# The below commands load the ANES data into your R Studio session
#  The ability to make use of this will depend on how you got the
#  original data files. To use these functions as is, simply place
#  the downloaded zip file into a folder called `data/` and name 
#  output of the zip as `ANES2020/`

data_file <- dir(
  here::here("data/ANES2020"),
  pattern    = ".+.dta$",    # Finds Stata Files
  full.names = TRUE,         # List Full File Path Names
  recursive  = TRUE)         # Repeat if needed


ANES_2020 <- read_dta(
  data_file
) %>% 
  zap_labels()

# *****************************************************************
# CLEAN DATA ####
# *****************************************************************

# This following code generates a cleaned version of the ANES data
#   that we loaded above and provides a survey object for which
#   we can work with to get the summary statistics and regression
#   results

source("ANES20_CLEAN.R")

# *****************************************************************
# DESCRIPTIVE STATISTICS ####
# *****************************************************************

# The following code generates:
#   - Figures 1 and 2 in the main paper
#   - Chi-Squared tables in Supplemental Appendix C

source("DESCRIPTIVE.R")

# *****************************************************************
# REGRESSION MODELS ####
# *****************************************************************

# The following code gets the regression results for the main paper
#   This generates:
#   - Figure 3 in the main paper
#   - Tables in Supplemetal Appendix D

source("REGRESSION.R")

# The following code generates regression results for the 
#   Supplemental Appendix. This provides:
#   - Figure in Supplemental Appendix E
#   - Tables in Supplemental Appendix E

source("REGRESSION_SI.R")
